package com.neusoft.elmboot.controller;

import com.neusoft.elmboot.po.User;
import com.neusoft.elmboot.service.SuperMemberService;
import com.neusoft.elmboot.util.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/elm/SuperMemberController")
public class SuperMemberController {
	
	@Autowired
	private SuperMemberService superMemberService;
	
	@RequestMapping("/status")
	public ResultBody getSuperMemberStatus(@RequestParam String userId) {
		try {
			User user = superMemberService.getSuperMemberStatus(userId);
			if (user == null) {
				return ResultBody.error("用户不存在", null);
			}
			return ResultBody.success("获取成功", user);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("获取失败：" + e.getMessage(), null);
		}
	}
	
	@RequestMapping("/purchase")
	public ResultBody purchaseSuperMember(@RequestParam String userId, @RequestParam int duration) {
		try {
			boolean success = superMemberService.purchaseSuperMember(userId, duration);
			if (success) {
				return ResultBody.success("购买成功", null);
			} else {
				return ResultBody.error("购买失败", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("购买失败：" + e.getMessage(), null);
		}
	}
	
	@RequestMapping("/check")
	public ResultBody checkSuperMember(@RequestParam String userId) {
		try {
			boolean isValid = superMemberService.isValidSuperMember(userId);
			return ResultBody.success("检查完成", isValid);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultBody.error("检查失败：" + e.getMessage(), null);
		}
	}
}

